package com.llc.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.llc.common.exception.BizCodeEnume;
import com.llc.gulimall.member.exception.PhoneException;
import com.llc.gulimall.member.exception.UsernameException;
import com.llc.gulimall.member.feign.CouponFeignService;
import com.llc.gulimall.member.vo.MemberUserLoginVo;
import com.llc.gulimall.member.vo.MemberUserRegisterVo;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.llc.gulimall.member.entity.MemberEntity;
import com.llc.gulimall.member.service.MemberService;
import com.llc.common.utils.PageUtils;
import com.llc.common.utils.R;



/**
 * 会员
 *
 * @author lls
 * @email leilingchun@163.com
 * @date 2022-01-24 17:26:04
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    //远程调用测试
    @Autowired
    CouponFeignService couponFeignService;


    /**
     *
     * @author llc
     * @description  登录
     * @date 2022/2/13 21:14
     * @param vo
     * @return com.llc.common.utils.R
     */

    @PostMapping(value = "/login")
    public R login(@RequestBody MemberUserLoginVo vo) {

        MemberEntity memberEntity = memberService.login(vo);
        System.out.println("");
        if (memberEntity != null) {
            return R.ok().setData(memberEntity);
        } else {
            return R.error(BizCodeEnume.LOGIN_ACCOUNT_PASSWORD_EXCEPTION.getCode(), BizCodeEnume.LOGIN_ACCOUNT_PASSWORD_EXCEPTION.getMsg());
        }
    }



    /**
     *
     * @author llc
     * @description  用于账号注册
     * @date 2022/2/13 20:55
     * @param vo
     * @return com.llc.common.utils.R
     */

    @PostMapping(value = "/register")
    public R register(@RequestBody MemberUserRegisterVo vo) {

        try {
            System.out.println("开始远程调用");
            memberService.register(vo);
        } catch (PhoneException e) {
            return R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UsernameException e) {
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(), BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }

        return R.ok();
    }

    @RequestMapping("/coupons")
    public R test(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");

        R membercoupons = couponFeignService.membercoupons();
        return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));
    }



    /**
     * 列表
     */
    @RequestMapping("/list")
        public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
        public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
        public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
        public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/test2")
    public String delete(){
        return "测试结果";
    }

}
